<?php
namespace app\index\controller;

use think\facade\Session;
use app\index\controller\Base;
use app\common\model\User as UserModel;
use app\common\model\SmsLog as SmsLogModel;

class Register extends Base
{
    public function index(){
        $this->redirect('index/index');
    }

    /**
     * 用户注册
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-27
     * @return   [type]             [description]
     */
    public function create(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }

        if(!empty($this->current_user)){
            $this->success("您已登录", url('index/index'), 0);
        }

        $fields = [
            ['name' => 'name', 'type' => 'string', 'default' => ''],
            ['name' => 'mobile', 'type' => 'integer', 'default' => 0],
            ['name' => 'code', 'type' => 'integer', 'default' => 0],
            ['name' => 'password', 'type' => 'string', 'default' => ''],
        ];

        $data = $this->parsePostParam($fields);
        $SmsLogModel = new SmsLogModel();
        $valid_res = $SmsLogModel->validSendCode($data['mobile'], $data['code']);
        if(!$valid_res['status']){
            $this->error($valid_res['msg']);
        }

        $data['confirm_password'] = $data['password'];
        $Model = new UserModel();
        $create_res = $Model->createItem($data);

        if(!$create_res['status']){
            $this->error($create_res['msg']);
        }
        $login_res = $Model->doLogin($data['name'], $data['password']);
        if(!$login_res['status']){
            $this->error("注册并登录失败");
        }else{
            Session::set("user", $login_res['data']);
            $this->success("注册并登录成功", url('index/index'), 0);
        }
    }

    /**
     * 用户重置密码
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-27
     * @return   [type]             [description]
     */
    public function reset_password(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }

        $fields = [
            ['name' => 'mobile', 'type' => 'string', 'default' => ''],
            ['name' => 'code', 'type' => 'string', 'default' => ''],
            ['name' => 'password', 'type' => 'string', 'default' => ''],
        ];
        $data = $this->parsePostParam($fields);
        $SmsLogModel = new SmsLogModel();
        $valid_res = $SmsLogModel->validSendCode($data['mobile'], $data['code']);
        if(!$valid_res['status']){
            return json($valid_res);
            $this->error($valid_res['msg']);
        }

        $UserModel = new UserModel();
        $reset_res = $UserModel->resetPassword($data['mobile'], $data['password']);
        if($reset_res['status']){
            $this->success($reset_res['msg']);
        }else{
            $this->error($reset_res['msg']);
        }
    }

    public function send_code(){
        if(!$this->request->isPost()){
            if($this->request->isAjax()){
                $this->error('访问的页面不存在', url('index/index'));
            }else{
                $this->redirect('index/index');
            }
        }

        $message_type = $this->request->post('message_type');
        $mobile = $this->request->post('mobile');

        $SmsLogModel = new SmsLogModel();
        switch ($message_type) {
            case 'register':
                $send_res = $SmsLogModel->sendRegisterMessage($mobile);
                break;
            case 'change_mobile':
                $send_res = $SmsLogModel->sendChangeMobileMessage($mobile);
                break;
            case 'reset_password':
                $send_res = $SmsLogModel->sendResetInfoMessage($mobile);
                break;
            default:
                // 使用当前登录用户的手机号码
                $send_res = $SmsLogModel->sendResetInfoMessage();
                break;
        }
        if($send_res['status']){
            $this->success($send_res['msg']);
        }else{
            $this->error($send_res['msg']);
        }
    }


    public function send_sms(){
        $SmsLogModel = new SmsLogModel();
        $res = $SmsLogModel->testSendBySimple();
        return json($res);
    }

    public function valid_sms(){
        $SmsLogModel = new SmsLogModel();
        $res = $SmsLogModel->testValidBySimple();
        return json($res);
    }
}
